﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using Microsoft.LightSwitch;
using System.Xml.Xsl;
using System.Xml;
using System.Xml.Linq;

namespace LightSwitchApplication
{
    public partial class DownloadFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Get parameters
            Request.ContentEncoding = System.Text.Encoding.Unicode;

            string id = Request.QueryString["OrderId"];
            string xml = Utils.SimpleDecoder(Request.QueryString["src"], string.Empty);

            if (xml != "")
            {
                try
                {
                    Response.ClearHeaders();
                    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.html", id));

                    Response.ClearContent();
                    Response.ContentEncoding = System.Text.Encoding.Unicode;
                    Response.ContentType = GetContentType("html");

                    XDocument xmlDoc = XDocument.Load(new StringReader(xml));
                    XslCompiledTransform xslt = new XslCompiledTransform();
                    //string xslString = LightSwitchApplication.Server.DataAccess.GetSystemParameterValue(@"SalesOrder.StyleSheet");
                    //if (!string.IsNullOrEmpty(xslString))
                    //{
                    //    XDocument xslDoc = XDocument.Load(new StringReader(xslString));
                    //    xslt.Load(xslDoc.CreateReader());
                    //}
                    //else
                    //{
                        xslt.Load("SalesOrder.xsl");
                    //}

                    using (MemoryStream stream = new MemoryStream())
                    {
                        using (var writer = XmlWriter.Create(stream, xslt.OutputSettings))
                        {
                            xslt.Transform(xmlDoc.CreateReader(), writer);
                        };
                        var arr = stream.ToArray();
                        Response.Write(xslt.OutputSettings.Encoding.GetString(arr));
                    }
                    Response.End();
                }
                catch (Exception ex)
                {
                    lblDisplayFilesError.Text = ex.Message;
                }
            }

        }

        private string SimpleDecoder(string encodedString)
        {
            if (!string.IsNullOrEmpty(encodedString))
            {
                byte[] b = Convert.FromBase64String(encodedString.Replace(' ', '+')); // + "=="
                string strOriginal = System.Text.Encoding.UTF8.GetString(b, 0, b.Length);
                return strOriginal;
            }
            else
            {
                return string.Empty;
            }
        }

        #region GetContentType
        public string GetContentType(string strextension)
        {
            if (!strextension.StartsWith("."))
                strextension = "."+strextension;

            string contentType;
            switch (strextension.ToLower())
            {
                case ".gif":
                    contentType = "image/gif";
                    break;
                case ".jpg":
                case ".jpeg":
                    contentType = "image/jpeg";
                    break;
                case ".png":
                    contentType = "image/png";
                    break;
                case ".doc":
                    contentType = "application/ms-word";
                    break;
                case ".docx":
                    contentType = "application/vnd.ms-word.document.12";
                    break;
                case ".pdf":
                    contentType = "application/pdf";
                    break;
                case ".xls":
                    contentType = "application/vnd.ms-excel";
                    break;
                case ".ppt":
                    contentType = "application/vnd.ms-powerpoint";
                    break;
                case ".zip":
                    contentType = "application/zip";
                    break;
                case ".html":
                case ".htm":
                    contentType = "text/html";
                    break;
                case ".xml":
                case ".txt":
                case ".ics":
                    contentType = "text/plain";
                    break;
                default:
                    contentType = "application/octet-stream";
                    break;
            }
            return contentType;
        }
        #endregion
    }
}